package com.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.entity.Dict;
import com.edu.mapper.DictMapper;
import com.edu.service.DictService;
import com.edu.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @ClassName DictServiceImpl
 * @Description
 * @Author W.Jian
 * @Date 2021/5/23 14:13
 **/
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Autowired
    private DictMapper dictMapper;


    @Override
    public Result listByTypeLike(String type, String value) {
        if(StringUtils.isEmpty(type)){
            return Result.failure("500","参数缺失！");
        }
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag",0);
        queryWrapper.eq("type", type);
        if(!StringUtils.isEmpty(value)){
            queryWrapper.like("value", value);
        }
        List<Dict> dicts = dictMapper.selectList(queryWrapper);
        return Result.success(dicts,dicts.size());
    }

    @Override
    public Result listByType(String type) {
        if(StringUtils.isEmpty(type)){
            return Result.failure("500","参数缺失！");
        }
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag",0);
        queryWrapper.eq("type", type);
        List<Dict> dicts = dictMapper.selectList(queryWrapper);
        return Result.success(dicts,dicts.size());
    }

    /**
     * 批量添加
     * @param dictList
     * @return
     */
    @Override
    public Result insertBatch(List<Dict> dictList){
        if(CollectionUtils.isEmpty(dictList)){
            return Result.failure("500","参数缺失");
        }
        dictList.forEach(dict->{
            dictMapper.insert(dict);
        });
        return Result.success();
    }

    /**
     * 条件查询 判断字典项是否存在
     * @param dict
     * @return
     */
    @Override
    public Result selectByCondition(Dict dict) {
        if(StringUtils.isEmpty(dict)){
            return Result.failure("500","参数缺失！");
        }
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag",0);
        queryWrapper.eq("type", dict.getType());
        queryWrapper.eq("value", dict.getValue());
        List<Dict> dicts = dictMapper.selectList(queryWrapper);
        return Result.success(dicts,dicts.size());
    }
}
